﻿#region Apache License
/* 
   Copyright 2011 Jorge Teixeira

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdHoc.Wavesque
{
    /// <summary>
    /// Base class for sinusoids.
    /// </summary>
    public abstract class SinusoidesqueBase : Esque
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SinusoidesqueBase"/> class.
        /// </summary>
        protected SinusoidesqueBase()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SinusoidesqueBase"/> class.
        /// </summary>
        /// <param name="step">Value for position increases.</param>
        /// <param name="amplitude">Amplitude of the oscillation.</param>
        /// <param name="frequency">Frequency of the oscillation (in Hz).</param>
        /// <param name="phase">Phase of the oscillation (in radians).</param>
        protected SinusoidesqueBase(double step, double amplitude, double frequency, double phase)
        {
            Step = step;
            Amplitude = amplitude;
            Frequency = frequency;
            Phase = phase;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SinusoidesqueBase"/> class.
        /// </summary>
        /// <param name="amplitude">Amplitude of the oscillation.</param>
        /// <param name="frequency">Frequency of the oscillation (in Hz).</param>
        /// <param name="phase">Phase of the oscillation (in radians).</param>
        protected SinusoidesqueBase(double amplitude, double frequency, double phase)
        {
            if (frequency != 0.0)
            {
                Step = EsqueHelpers.GetStepDefaultValue(frequency);
            }
            Amplitude = amplitude;
            Frequency = frequency;
            Phase = phase;
        }

        /// <summary>
        /// Calculates a given number of points of a waveform for a given initial position.
        /// </summary>
        /// <param name="numberPoints">Number of points to compute.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        /// <param name="x0">Initial position.</param>
        public override void GetValues(uint numberPoints, out double[] x, out double[] y, double x0)
        {
            double cX = x0;
            double cY = 0.0;
            
            if (numberPoints > 0)
            {
                x = new double[numberPoints];
                y = new double[numberPoints];
            }
            else
            {
                x = new double[1];
                y = new double[1];

                x[0] = x0;
                y[0] = 0.0;
            }
       
            if (Frequency <= 0.0)
                return;

            double period = 1.0 / Frequency;
            double w = (2.0 * Math.PI) / period;
            for (int i = 0; i < numberPoints; i++)
            {
                if (i != 0)
                {
                    cX = cX + Step;
                }
                
                cY = Amplitude * Math.Sin((w * cX) + Phase);

                x[i] = cX;
                y[i] = cY;
            }
        }
    }
}
